#include "scheduler.h"

using namespace zsylar;

static unsigned int test_number;
std::mutex mutex_cout;

void task()
{
    {
        // 此处加锁是保证线程的顺序
        std::lock_guard<std::mutex> lock(mutex_cout);
        std::cout << "task " << test_number++ << " is under processing in thread: " 
                  << Thread::GetThreadId() << std::endl;
    }
    sleep(1);
}

int main(int argc, char const *argv[])
{
    {
        std::shared_ptr<Scheduler> scheduler = std::make_shared<Scheduler>(3, true, "scheduler_1");
        scheduler->start(); // 启动调度器-创建线程池

        sleep(2);

        std::cout << "\nbegin post\n\n";
        for (int i = 0; i < 5; i++)
        {
            std::shared_ptr<Fiber> fiber = std::make_shared<Fiber>(task);
            scheduler->scheduleLock(fiber); // 添加任务到调度器
        }

        sleep(6);

        std::cout << "\npost again\n\n";
        for (int i = 0; i < 15; i++)
        {
            std::shared_ptr<Fiber> fiber = std::make_shared<Fiber>(task);
            scheduler->scheduleLock(fiber); // 再次添加任务到调度器
        }

        sleep(3);
        scheduler->stop(); // 停止调度器
    }
    return 0;
}